package core;

import java.io.File;
import java.lang.reflect.Method;

import junit.framework.TestCase;

import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import reporting.Report;
import annotations.Browser;
import annotations.EntryPoint;
import enums.Browsers;

public class BaseTestCase extends TestCase {

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		PropertiesManager.initialise();
		executePrerequisites();
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		WebTestContext.getWebDriver().quit();
	}

	private void executePrerequisites() {
		String url = null;
		Browsers browser = null;

		// Check whether test class has Entry Point annotation
		EntryPoint classEntryPoint = this.getClass().getAnnotation(
				EntryPoint.class);
		if (classEntryPoint != null) {
			url = classEntryPoint.value();
			if(!url.contains("http:") || !url.contains("https:"))
			{
				//url = getClass().getResource("examples/") + url;
				url = System.getProperty("user.dir") + "\\examples\\" + url;
			}
		}

		// Check whether test class has Browser annotation
		Browser classBrowser = this.getClass().getAnnotation(Browser.class);
		if (classBrowser != null) {
			browser = classBrowser.value();
		}

		Method[] methods = this.getClass().getMethods();
		if (methods != null) {

			// Check whether the test method has browser and entry point
			// assigned
			// Take into account that the test methods annotations override test
			// class annotations
			for (Method method : methods) {

				// Check whether current method has Browser annotation
				Browser methodBrowser = method.getAnnotation(Browser.class);
				if (methodBrowser != null) {
					browser = methodBrowser.value();
				}

				// Check whether current method has EntryPoint annotation
				EntryPoint methodEntryPoint = method
						.getAnnotation(EntryPoint.class);
				if (methodEntryPoint != null) {
					url = methodEntryPoint.value();
					if(!url.contains("http:") || !url.contains("https:"))
					{
						url = System.getProperty("user.dir") + "\\examples\\" + url;
					}
				}
			}
		}

		// Check whether browser and entry point are set in test methods or in
		// test class
		if (url != null && browser != null) {
			// Open passed URL in selected browser
			openBrowser(url, browser);
		} else {
			Report.error("Browser or Entry Point are not set!");
		}
	}

	private void openBrowser(String url, Browsers browser) {

		switch (browser) {
		case Firefox: {
			FirefoxBinary binary = new FirefoxBinary(new File(
					PropertiesManager.getString("path.firefox")));
			FirefoxProfile profile = new FirefoxProfile();
			WebTestContext.setWebDriver(new FirefoxDriver(binary, profile));
		}
			break;
		case InternetExplorer: {

		}
			break;

		default: {

		}
			break;
		}

		WebTestContext.getWebDriver().get(url);
	}
}
